//
//  EFengTabButton.m
//  WZEfengAndEtong
//
//  Created by wanzhao on 16/1/20.
//  Copyright © 2016年 wanzhao. All rights reserved.
//

#import "EFengTabButton.h"
#import "IWBadgeButton.h"

@interface EFengTabButton ()

@property(nonatomic, weak)IWBadgeButton *badgeBtn;
@property (nonatomic, weak) UIView *redView;

@end

@implementation EFengTabButton

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // 1.设置按钮的属性
        // 设置按钮图片居中显示
        self.imageView.contentMode = UIViewContentModeCenter;
        // 设置按钮标题居中显示
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        
        [self setExclusiveTouch:YES];/*禁用按钮同时点击*/
        // 2.添加提醒按钮
        IWBadgeButton *badgeBtn = [[IWBadgeButton alloc] init];
        [self addSubview:badgeBtn];
        self.badgeBtn = badgeBtn;
        badgeBtn.hidden = YES;

        UIView *view = [[UIView alloc] init];
        view.layer.cornerRadius = 5.0f;
        view.layer.masksToBounds = YES;
        view.backgroundColor = RedColor;
        view.hidden = YES;
        [self addSubview:view];
        self.redView = view;
    }
    return self;
}

- (CGRect)imageRectForContentRect:(CGRect)contentRect
{
    CGFloat imgX = 0;
    CGFloat imgY = self.contentEdgeInsets.top;
    CGFloat imgW = self.width;
    CGFloat imgH = self.currentImage.size.height;
    return CGRectMake(imgX, imgY, imgW, imgH);
}

- (CGRect)titleRectForContentRect:(CGRect)contentRect
{
    CGFloat titleX = 0;
    CGFloat titleY = self.currentImage.size.height + self.contentEdgeInsets.top;
    CGFloat titleW = self.width;
    CGFloat titleH = self.height - self.currentImage.size.height - self.contentEdgeInsets.top;
    return CGRectMake(titleX, titleY, titleW, titleH);
}


- (void)setHighlighted:(BOOL)highlighted
{
}

- (void)setItem:(EFengAndETongTabBarItem *)item
{
    _item = item;
    [self setNeedsLayout];
    // 注册监听, 监听badgeValue的改变
    [_item addObserver:self forKeyPath:@"badgeValue" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld  context:@"xxoo"];
    [_item addObserver:self forKeyPath:@"redDianValue" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld  context:@"xxoo"];
}

- (void)dealloc
{
    [_item removeObserver:self forKeyPath:@"badgeValue"];
    [_item removeObserver:self forKeyPath:@"redDianValue"];
}

/**
 * 当监听到对象的属性改变之后就会自动调用
 * keyPath: 被监听的属性
 * object: 被监听的对象
 * change: 修改之后的一些数据(可能时新值, 也可能时旧值, 也可能新旧都有)
 * context 当初注册监听的时候传递的参数
 */
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    
    self.badgeBtn.item = self.item;
    [self setNeedsLayout];
    if (self.item.badgeValue.integerValue > 0) {
        self.redView.hidden = YES;
    } else {
        if (self.item.redDianValue.integerValue > 0) {
            self.redView.hidden = NO;
        } else {
            self.redView.hidden = YES;
        }

    }
    
}



- (void)layoutSubviews
{
    [super layoutSubviews];
    self.badgeBtn.size = self.badgeBtn.currentBackgroundImage.size;
    self.badgeBtn.x = self.imageView.centerX + self.currentImage.size.width / 2.0f - 7.0f;
    self.badgeBtn.y = 0.0f;
    
    self.redView.frame = CGRectMake(self.imageView.centerX + self.currentImage.size.width / 2.0f - 2.0f, 6.0f, 10.0f, 10.0f);
}

@end
